{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:32.057901Z",
     "start_time": "2024-12-23T03:12:32.049775Z"
    }
   },
   "source": [
    ""
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:32.211850Z",
     "start_time": "2024-12-23T03:12:32.203598Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import matplotlib.pyplot as plt"
   ],
   "id": "9c201dd5d1296eae",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:34.108085Z",
     "start_time": "2024-12-23T03:12:32.212916Z"
    }
   },
   "cell_type": "code",
   "source": [
    "raw_df_15 = pd.read_csv('./data/15_05.csv')\n",
    "raw_df_15 = raw_df_15[raw_df_15['label'] != -1]\n",
    "raw_df_21 = pd.read_csv('./data/21_05.csv')\n",
    "raw_df_21 = raw_df_21[raw_df_21['label'] != -1]\n",
    "\n"
   ],
   "id": "9f32f6ea7026257c",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:34.124025Z",
     "start_time": "2024-12-23T03:12:34.109534Z"
    }
   },
   "cell_type": "code",
   "source": "drop_columns = ['time', 'group', 'new_group']",
   "id": "a680e05cbce1a759",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:34.170895Z",
     "start_time": "2024-12-23T03:12:34.125061Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_15 = raw_df_15.drop(drop_columns, axis=1) \n",
    "df_21 = raw_df_21.drop(drop_columns, axis=1)"
   ],
   "id": "b99e5013a9bbff74",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 相关性分析",
   "id": "3a127e085242c69c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:34.186269Z",
     "start_time": "2024-12-23T03:12:34.171896Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def correlation_analyse(df):\n",
    "    corr = df.corr()\n",
    "    target_corr = corr['label'].drop('label')\n",
    "    target_corr = target_corr.abs().sort_values(ascending=False)\n",
    "    \n",
    "    filtered_features = target_corr[target_corr > 0.05]\n",
    "    print(filtered_features)\n",
    "    return filtered_features"
   ],
   "id": "ac76e66796ac8365",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:35.019783Z",
     "start_time": "2024-12-23T03:12:34.187272Z"
    }
   },
   "cell_type": "code",
   "source": [
    "corr_features_15 = correlation_analyse(df_15)\n",
    "print(len(corr_features_15))"
   ],
   "id": "64f38e0835cdde0f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r_wind_speed_to_power              0.426171\n",
      "r_square                           0.411995\n",
      "r_wind_speed_to_generator_speed    0.329559\n",
      "environment_tmp                    0.163251\n",
      "generator_speed                    0.160714\n",
      "power                              0.156338\n",
      "int_tmp                            0.136187\n",
      "wind_speed_square                  0.085951\n",
      "tmp_diff                           0.075170\n",
      "pitch3_moto_tmp                    0.072061\n",
      "pitch1_moto_tmp                    0.070486\n",
      "pitch_moto_tmp_mean                0.070033\n",
      "pitch2_moto_tmp                    0.067421\n",
      "pitch_moto_tmp_sd                  0.064064\n",
      "pitch3_angle                       0.050609\n",
      "Name: label, dtype: float64\n",
      "15\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:35.424140Z",
     "start_time": "2024-12-23T03:12:35.020939Z"
    }
   },
   "cell_type": "code",
   "source": [
    "corr_features_21 = correlation_analyse(df_21)\n",
    "print(len(corr_features_21))"
   ],
   "id": "ff06a1de5737ee19",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r_wind_speed_to_power              0.331405\n",
      "tmp_diff                           0.250775\n",
      "r_square                           0.230897\n",
      "environment_tmp                    0.203861\n",
      "power                              0.157785\n",
      "r_wind_speed_to_generator_speed    0.149098\n",
      "generator_speed                    0.144542\n",
      "int_tmp                            0.134756\n",
      "pitch3_moto_tmp                    0.109280\n",
      "pitch_moto_tmp_mean                0.107200\n",
      "pitch1_moto_tmp                    0.106239\n",
      "pitch2_moto_tmp                    0.105842\n",
      "wind_speed_square                  0.077548\n",
      "pitch1_angle                       0.071772\n",
      "pitch2_angle                       0.070353\n",
      "pitch_angle_mean                   0.070137\n",
      "pitch3_angle                       0.068288\n",
      "pitch_speed_sd                     0.067357\n",
      "yaw_position                       0.065936\n",
      "wind_speed                         0.058847\n",
      "wind_speed_cube                    0.057203\n",
      "Name: label, dtype: float64\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:35.440153Z",
     "start_time": "2024-12-23T03:12:35.424140Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 交集\n",
    "corr_features = set(corr_features_15.index).intersection(set(corr_features_21.index))\n",
    "print(len(corr_features)) \n",
    "print(corr_features)"
   ],
   "id": "a3dbcaffc0ff5af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "{'power', 'tmp_diff', 'pitch1_moto_tmp', 'r_wind_speed_to_power', 'pitch3_moto_tmp', 'pitch3_angle', 'int_tmp', 'r_square', 'wind_speed_square', 'environment_tmp', 'generator_speed', 'r_wind_speed_to_generator_speed', 'pitch_moto_tmp_mean', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 单变量特征选择",
   "id": "d67f1380a243d852"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:35.470373Z",
     "start_time": "2024-12-23T03:12:35.441156Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.feature_selection import SelectKBest, chi2, mutual_info_classif\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ],
   "id": "199d0fdad60b1a2f",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:12:35.485612Z",
     "start_time": "2024-12-23T03:12:35.471418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def univariate_feature_selection(df):\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('label', axis=1)\n",
    "    y = df['label']\n",
    "    \n",
    "    # 将特征缩放到 [0, 1] 范围\n",
    "    scaler = MinMaxScaler()\n",
    "    X_scaled = scaler.fit_transform(X)\n",
    "    \n",
    "    # 使用卡方检验进行特征选择\n",
    "    chi2_selector = SelectKBest(score_func=chi2, k='all')\n",
    "    chi2_selector.fit(X_scaled, y)\n",
    "    \n",
    "    # 获取所有特征的得分\n",
    "    chi2_scores = chi2_selector.scores_\n",
    "    \n",
    "    # 设置分数阈值\n",
    "    threshold = 50  # 设定的分数阈值\n",
    "    selected_features_chi2 = X.columns[chi2_scores > threshold]\n",
    "    selected_scores_chi2 = chi2_scores[chi2_scores > threshold]\n",
    "    # 创建 DataFrame 并排序\n",
    "    chi2_results = pd.DataFrame({'Feature': selected_features_chi2, 'Score': selected_scores_chi2})\n",
    "    chi2_results = chi2_results.sort_values(by='Score', ascending=False)\n",
    "    # 打印特征及其分数\n",
    "    print(\"根据分数阈值选择的特征及其分数（卡方检验）:\")\n",
    "    print(chi2_results)\n",
    "    \n",
    "    # 使用互信息进行特征选择\n",
    "    mi_selector = SelectKBest(score_func=mutual_info_classif, k='all')\n",
    "    mi_selector.fit(X, y)\n",
    "    \n",
    "    # 获取所有特征的得分\n",
    "    mi_scores = mi_selector.scores_\n",
    "    \n",
    "    # 设置分数阈值\n",
    "    threshold_mi = 0.03  # 设定的分数阈值\n",
    "    selected_features_mi = X.columns[mi_scores > threshold_mi]\n",
    "    selected_scores_mi = mi_scores[mi_scores > threshold_mi]\n",
    "    \n",
    "    # 创建 DataFrame 并排序\n",
    "    mi_results = pd.DataFrame({'Feature': selected_features_mi, 'Score': selected_scores_mi})\n",
    "    mi_results = mi_results.sort_values(by='Score', ascending=False)\n",
    "    \n",
    "    # 打印特征及其分数\n",
    "    print(\"根据分数阈值选择的特征及其分数（互信息）:\")\n",
    "    print(mi_results)\n",
    "    \n",
    "    return selected_features_chi2, selected_features_mi"
   ],
   "id": "62ee9da39d9f055f",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:20.179547Z",
     "start_time": "2024-12-23T03:12:35.486614Z"
    }
   },
   "cell_type": "code",
   "source": "chi2_features_15, mi_features_15 = univariate_feature_selection(df_15)",
   "id": "cbdb99bf86e89606",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据分数阈值选择的特征及其分数（卡方检验）:\n",
      "                            Feature        Score\n",
      "1                             power  2171.447495\n",
      "17                         r_square  2152.656357\n",
      "15            r_wind_speed_to_power  1660.365473\n",
      "16  r_wind_speed_to_generator_speed  1615.699717\n",
      "0                   generator_speed  1076.940116\n",
      "8                   environment_tmp   910.392128\n",
      "9                           int_tmp   688.395402\n",
      "11                   pitch_speed_sd   269.001192\n",
      "18                wind_speed_square   257.647604\n",
      "4                      pitch3_angle   181.367007\n",
      "10                 pitch_angle_mean   181.199658\n",
      "2                      pitch1_angle   171.689718\n",
      "5                   pitch1_moto_tmp   163.677341\n",
      "3                      pitch2_angle   162.264464\n",
      "7                   pitch3_moto_tmp   158.986729\n",
      "12              pitch_moto_tmp_mean   157.393273\n",
      "6                   pitch2_moto_tmp   147.596572\n",
      "13                pitch_moto_tmp_sd   104.098038\n",
      "14                         tmp_diff    66.814013\n",
      "根据分数阈值选择的特征及其分数（互信息）:\n",
      "                            Feature     Score\n",
      "12                         tmp_diff  0.136760\n",
      "11              pitch_moto_tmp_mean  0.080264\n",
      "14            r_wind_speed_to_power  0.078256\n",
      "0                      yaw_position  0.068566\n",
      "16                         r_square  0.063874\n",
      "9                  pitch_angle_mean  0.059466\n",
      "15  r_wind_speed_to_generator_speed  0.057011\n",
      "10                   pitch_angle_sd  0.056761\n",
      "13                           lambda  0.055289\n",
      "1                      pitch1_angle  0.046439\n",
      "2                      pitch2_angle  0.042548\n",
      "7                   environment_tmp  0.037915\n",
      "4                   pitch1_moto_tmp  0.036176\n",
      "5                   pitch2_moto_tmp  0.036047\n",
      "6                   pitch3_moto_tmp  0.035911\n",
      "3                      pitch3_angle  0.033184\n",
      "8                           int_tmp  0.032366\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:39.828801Z",
     "start_time": "2024-12-23T03:13:20.180549Z"
    }
   },
   "cell_type": "code",
   "source": "chi2_features_21, mi_features_21 = univariate_feature_selection(df_21)",
   "id": "426f642a7f512f6d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据分数阈值选择的特征及其分数（卡方检验）:\n",
      "                            Feature       Score\n",
      "1                             power  885.714888\n",
      "0                   generator_speed  669.741777\n",
      "15            r_wind_speed_to_power  625.653261\n",
      "9                   environment_tmp  541.619812\n",
      "17                         r_square  430.513540\n",
      "14                         tmp_diff  332.355700\n",
      "3                      pitch1_angle  274.537410\n",
      "4                      pitch2_angle  267.562200\n",
      "11                 pitch_angle_mean  265.597590\n",
      "5                      pitch3_angle  252.287702\n",
      "12                   pitch_speed_sd  244.966891\n",
      "10                          int_tmp  219.272151\n",
      "16  r_wind_speed_to_generator_speed  209.055369\n",
      "18                wind_speed_square  201.440622\n",
      "8                   pitch3_moto_tmp  198.921148\n",
      "13              pitch_moto_tmp_mean  187.086688\n",
      "7                   pitch2_moto_tmp  180.610613\n",
      "6                   pitch1_moto_tmp  179.382223\n",
      "2                      yaw_position   94.055729\n",
      "根据分数阈值选择的特征及其分数（互信息）:\n",
      "                  Feature     Score\n",
      "13               tmp_diff  0.160921\n",
      "9        pitch_angle_mean  0.129404\n",
      "11    pitch_moto_tmp_mean  0.129313\n",
      "0            yaw_position  0.092585\n",
      "2            pitch2_angle  0.084544\n",
      "4         pitch1_moto_tmp  0.074481\n",
      "1            pitch1_angle  0.073145\n",
      "6         pitch3_moto_tmp  0.071124\n",
      "10         pitch_angle_sd  0.069396\n",
      "7         environment_tmp  0.068707\n",
      "5         pitch2_moto_tmp  0.068206\n",
      "3            pitch3_angle  0.049439\n",
      "14  r_wind_speed_to_power  0.048375\n",
      "8                 int_tmp  0.045564\n",
      "12      pitch_moto_tmp_sd  0.041799\n",
      "15               r_square  0.031459\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:39.844127Z",
     "start_time": "2024-12-23T03:13:39.829308Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 交集\n",
    "chi2_features = set(chi2_features_15).intersection(set(chi2_features_21))\n",
    "print('卡方检验特征交集')\n",
    "print(len(chi2_features)) \n",
    "print(chi2_features)\n",
    "\n",
    "mi_features = set(mi_features_15).intersection(set(mi_features_21))\n",
    "print('互信息特征交集')\n",
    "print(len(mi_features))\n",
    "print(mi_features)"
   ],
   "id": "403e8b037cd28831",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "卡方检验特征交集\n",
      "18\n",
      "{'power', 'tmp_diff', 'pitch1_moto_tmp', 'r_wind_speed_to_power', 'pitch3_moto_tmp', 'pitch3_angle', 'int_tmp', 'pitch2_angle', 'wind_speed_square', 'pitch_angle_mean', 'pitch1_angle', 'r_square', 'environment_tmp', 'generator_speed', 'r_wind_speed_to_generator_speed', 'pitch_speed_sd', 'pitch_moto_tmp_mean', 'pitch2_moto_tmp'}\n",
      "互信息特征交集\n",
      "15\n",
      "{'tmp_diff', 'pitch1_moto_tmp', 'pitch3_angle', 'pitch2_angle', 'pitch3_moto_tmp', 'int_tmp', 'pitch_angle_mean', 'r_square', 'pitch1_angle', 'pitch_moto_tmp_mean', 'environment_tmp', 'pitch_angle_sd', 'yaw_position', 'r_wind_speed_to_power', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 逻辑回归",
   "id": "881095b40ac1b2fc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:39.848473Z",
     "start_time": "2024-12-23T03:13:39.845645Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import classification_report"
   ],
   "id": "eb34bd456dd962b3",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:39.863291Z",
     "start_time": "2024-12-23T03:13:39.850734Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def logistic_regression_analyse(df):\n",
    "\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('label', axis=1)\n",
    "    y = df['label']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n",
    "    \n",
    "    # 特征标准化\n",
    "    scaler = StandardScaler()\n",
    "    X_train_scaled = scaler.fit_transform(X_train)\n",
    "    X_test_scaled = scaler.transform(X_test)\n",
    "    \n",
    "    # 训练逻辑回归模型\n",
    "    model = LogisticRegression(max_iter=500)\n",
    "    model.fit(X_train_scaled, y_train)\n",
    "    \n",
    "    # 预测测试集\n",
    "    y_pred = model.predict(X_test_scaled)\n",
    "    \n",
    "    # 打印分类报告\n",
    "    print(\"分类报告:\")\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    # 提取特征重要性\n",
    "    importance = abs(model.coef_[0])  # 逻辑回归模型的系数\n",
    "    feature_names = X.columns  # 特征名称\n",
    "    \n",
    "    # 设置分数阈值\n",
    "    threshold = 1  # 设定的分数阈值\n",
    "    \n",
    "    # 选择重要特征\n",
    "    selected_indices = np.where(importance > threshold)[0]\n",
    "    selected_features = feature_names[selected_indices]\n",
    "    selected_importance = importance[selected_indices]\n",
    "    \n",
    "    # 创建 DataFrame 并排序\n",
    "    importance_df = pd.DataFrame({'Feature': selected_features, 'Score': selected_importance})\n",
    "    importance_df = importance_df.sort_values(by='Score', ascending=False)\n",
    "    \n",
    "    # 打印结果\n",
    "    print(\"\\n根据分数阈值选择的特征及其重要性（按分数降序排序）:\")\n",
    "    print(importance_df)\n",
    "    \n",
    "    return selected_features"
   ],
   "id": "15611213403d5700",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:43.521216Z",
     "start_time": "2024-12-23T03:13:39.864299Z"
    }
   },
   "cell_type": "code",
   "source": "lr_features_15 = logistic_regression_analyse(df_15)",
   "id": "a89d0f60197af83a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      0.99      0.98     70052\n",
      "           1       0.76      0.51      0.61      4778\n",
      "\n",
      "    accuracy                           0.96     74830\n",
      "   macro avg       0.87      0.75      0.80     74830\n",
      "weighted avg       0.95      0.96      0.95     74830\n",
      "\n",
      "\n",
      "根据分数阈值选择的特征及其重要性（按分数降序排序）:\n",
      "                            Feature      Score\n",
      "0                        wind_speed  16.339903\n",
      "4                      pitch2_angle  14.831769\n",
      "5                      pitch3_angle  14.194151\n",
      "6                   pitch1_moto_tmp  11.790316\n",
      "2                             power   9.380805\n",
      "11            r_wind_speed_to_power   8.870849\n",
      "7                   pitch2_moto_tmp   7.820954\n",
      "8                   pitch3_moto_tmp   5.079102\n",
      "13                         r_square   4.953764\n",
      "1                   generator_speed   4.865139\n",
      "12  r_wind_speed_to_generator_speed   3.644715\n",
      "14                wind_speed_square   2.410977\n",
      "3                      pitch1_angle   2.195405\n",
      "9                   environment_tmp   1.270338\n",
      "10                          int_tmp   1.171802\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:45.136745Z",
     "start_time": "2024-12-23T03:13:43.521929Z"
    }
   },
   "cell_type": "code",
   "source": "lr_features_21 = logistic_regression_analyse(df_21)",
   "id": "bb4647fe02e81d2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      0.99      0.98     33786\n",
      "           1       0.87      0.58      0.69      2128\n",
      "\n",
      "    accuracy                           0.97     35914\n",
      "   macro avg       0.92      0.79      0.84     35914\n",
      "weighted avg       0.97      0.97      0.97     35914\n",
      "\n",
      "\n",
      "根据分数阈值选择的特征及其重要性（按分数降序排序）:\n",
      "                            Feature      Score\n",
      "8                   pitch3_moto_tmp  28.513784\n",
      "5                      pitch3_angle  26.881187\n",
      "3                      pitch1_angle  17.465520\n",
      "7                   pitch2_moto_tmp  15.627581\n",
      "6                   pitch1_moto_tmp  13.303131\n",
      "4                      pitch2_angle  12.483316\n",
      "0                        wind_speed  12.046955\n",
      "1                   generator_speed   6.475215\n",
      "11  r_wind_speed_to_generator_speed   5.489316\n",
      "10            r_wind_speed_to_power   4.354028\n",
      "13                wind_speed_square   3.656537\n",
      "12                         r_square   3.528271\n",
      "2                             power   3.377348\n",
      "14                  wind_speed_cube   1.533047\n",
      "9                          tmp_diff   1.304647\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:45.152834Z",
     "start_time": "2024-12-23T03:13:45.137753Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr_features = set(lr_features_15).intersection(set(lr_features_21))\n",
    "print('逻辑回归特征交集')\n",
    "print(len(lr_features)) \n",
    "print(lr_features)"
   ],
   "id": "3abbd4286b36b36f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑回归特征交集\n",
      "13\n",
      "{'power', 'pitch1_moto_tmp', 'wind_speed', 'pitch3_angle', 'pitch2_angle', 'pitch3_moto_tmp', 'r_square', 'wind_speed_square', 'pitch1_angle', 'generator_speed', 'r_wind_speed_to_generator_speed', 'r_wind_speed_to_power', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 随机森林",
   "id": "4875853ba8c7af27"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:45.201894Z",
     "start_time": "2024-12-23T03:13:45.153840Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import classification_report"
   ],
   "id": "49567441286d1848",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:13:45.216638Z",
     "start_time": "2024-12-23T03:13:45.203925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def random_forest_analyse(df, df_test = None):\n",
    "\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('label', axis=1)\n",
    "    y = df['label']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n",
    "    \n",
    "    # 训练随机森林模型\n",
    "    model = RandomForestClassifier(random_state=42)\n",
    "    model.fit(X_train, y_train)\n",
    "    \n",
    "    # 预测测试集\n",
    "    y_pred = model.predict(X_test)\n",
    "    \n",
    "    # 打印分类报告\n",
    "    print(\"分类报告:\")\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    if df_test is not None:\n",
    "        X_test = df_test.drop('label', axis=1)\n",
    "        y_test = df_test['label']\n",
    "        \n",
    "        y_pred = model.predict(X_test)\n",
    "        print(\"另一个数据集的分类报告:\")\n",
    "        print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    # 提取特征重要性\n",
    "    importances = model.feature_importances_  # 随机森林的特征重要性\n",
    "    feature_names = X.columns  # 特征名称\n",
    "    \n",
    "    # 设置分数阈值\n",
    "    threshold = 0.04  # 设定的分数阈值\n",
    "    \n",
    "    # 根据阈值选择特征\n",
    "    selected_indices = np.where(importances > threshold)[0]\n",
    "    selected_features = feature_names[selected_indices]\n",
    "    selected_importance = importances[selected_indices]\n",
    "    \n",
    "    # 创建 DataFrame 并排序\n",
    "    importance_df = pd.DataFrame({'Feature': selected_features, 'Importance': selected_importance})\n",
    "    importance_df = importance_df.sort_values(by='Importance', ascending=False)\n",
    "    \n",
    "    # 打印结果\n",
    "    print(\"\\n根据分数阈值选择的特征及其重要性（按分数降序排序）:\")\n",
    "    print(importance_df)\n",
    "    \n",
    "    return selected_features"
   ],
   "id": "21d60f4665237f67",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:14:51.147283Z",
     "start_time": "2024-12-23T03:13:45.217648Z"
    }
   },
   "cell_type": "code",
   "source": "rf_features_15 = random_forest_analyse(df_15, df_21)",
   "id": "e47f436111e72797",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00     70052\n",
      "           1       1.00      1.00      1.00      4778\n",
      "\n",
      "    accuracy                           1.00     74830\n",
      "   macro avg       1.00      1.00      1.00     74830\n",
      "weighted avg       1.00      1.00      1.00     74830\n",
      "\n",
      "另一个数据集的分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.94      1.00      0.97    168930\n",
      "           1       0.57      0.08      0.13     10638\n",
      "\n",
      "    accuracy                           0.94    179568\n",
      "   macro avg       0.76      0.54      0.55    179568\n",
      "weighted avg       0.92      0.94      0.92    179568\n",
      "\n",
      "\n",
      "根据分数阈值选择的特征及其重要性（按分数降序排序）:\n",
      "                            Feature  Importance\n",
      "9             r_wind_speed_to_power    0.073251\n",
      "1                      yaw_position    0.067663\n",
      "11                         r_square    0.067622\n",
      "3                   environment_tmp    0.067272\n",
      "10  r_wind_speed_to_generator_speed    0.055753\n",
      "0                             power    0.054163\n",
      "4                           int_tmp    0.050423\n",
      "8                          tmp_diff    0.048493\n",
      "7               pitch_moto_tmp_mean    0.043781\n",
      "5                  pitch_angle_mean    0.042388\n",
      "6                    pitch_angle_sd    0.041749\n",
      "2                   pitch2_moto_tmp    0.040792\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:15:16.265795Z",
     "start_time": "2024-12-23T03:14:51.148573Z"
    }
   },
   "cell_type": "code",
   "source": "rf_features_21 = random_forest_analyse(df_21, df_15)",
   "id": "ed7075e30cc23da3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00     33786\n",
      "           1       1.00      1.00      1.00      2128\n",
      "\n",
      "    accuracy                           1.00     35914\n",
      "   macro avg       1.00      1.00      1.00     35914\n",
      "weighted avg       1.00      1.00      1.00     35914\n",
      "\n",
      "另一个数据集的分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.95      0.95      0.95    350255\n",
      "           1       0.28      0.26      0.27     23892\n",
      "\n",
      "    accuracy                           0.91    374147\n",
      "   macro avg       0.61      0.61      0.61    374147\n",
      "weighted avg       0.91      0.91      0.91    374147\n",
      "\n",
      "\n",
      "根据分数阈值选择的特征及其重要性（按分数降序排序）:\n",
      "                  Feature  Importance\n",
      "12               tmp_diff    0.077201\n",
      "7         environment_tmp    0.068801\n",
      "2            pitch1_angle    0.063179\n",
      "9        pitch_angle_mean    0.060337\n",
      "6         pitch3_moto_tmp    0.059195\n",
      "5         pitch2_moto_tmp    0.056197\n",
      "3            pitch2_angle    0.053644\n",
      "1            yaw_position    0.052548\n",
      "4         pitch1_moto_tmp    0.051223\n",
      "10         pitch_angle_sd    0.050377\n",
      "11    pitch_moto_tmp_mean    0.049986\n",
      "13  r_wind_speed_to_power    0.047839\n",
      "8                 int_tmp    0.042930\n",
      "0                   power    0.041614\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:15:16.280476Z",
     "start_time": "2024-12-23T03:15:16.265795Z"
    }
   },
   "cell_type": "code",
   "source": [
    "random_forest_features = set(rf_features_15).intersection(set(rf_features_21))\n",
    "print('随机森林特征交集')\n",
    "print(len(random_forest_features)) \n",
    "print(random_forest_features)"
   ],
   "id": "c42fb39a367cb298",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林特征交集\n",
      "10\n",
      "{'power', 'tmp_diff', 'pitch_moto_tmp_mean', 'int_tmp', 'pitch_angle_mean', 'environment_tmp', 'pitch_angle_sd', 'yaw_position', 'r_wind_speed_to_power', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 递归特征消除\n",
   "id": "eb0975aef9503172"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:15:16.295961Z",
     "start_time": "2024-12-23T03:15:16.281598Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.svm import SVC\n"
   ],
   "id": "1f6be00008ac6031",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:15:16.311345Z",
     "start_time": "2024-12-23T03:15:16.296968Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def rfe_analyse(df):\n",
    "    \n",
    "    # 提取特征和标签\n",
    "    X = df.drop('label', axis=1)\n",
    "    y = df['label']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "    \n",
    "    # 标准化\n",
    "    scaler = StandardScaler()\n",
    "    X_train = scaler.fit_transform(X_train)\n",
    "    X_test = scaler.transform(X_test)\n",
    "    \n",
    "    # 训练逻辑回归模型\n",
    "    model = SVC(kernel='linear', max_iter=500)  # 或者 'rbf', 'poly' 等\n",
    "    # model.fit(X_train, y_train)\n",
    "    \n",
    "    # 使用 RFE 进行特征选择\n",
    "    n = 15  # 选择的特征数量\n",
    "    rfe = RFE(model, n_features_to_select=n)\n",
    "    fit = rfe.fit(X_train, y_train)\n",
    "    \n",
    "    # 获取选择的特征\n",
    "    selected_features = X.columns[fit.support_]\n",
    "    \n",
    "    # 打印选择的特征\n",
    "    print(\"\\nrfe 选择的特征:\")\n",
    "    print(selected_features)\n",
    "    \n",
    "    return selected_features"
   ],
   "id": "89f050c49443852d",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:16.582745Z",
     "start_time": "2024-12-23T03:15:16.312381Z"
    }
   },
   "cell_type": "code",
   "source": "rfe_features_15 = rfe_analyse(df_15)",
   "id": "a4ac3c654eee0224",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "rfe 选择的特征:\n",
      "Index(['generator_speed', 'power', 'yaw_position', 'pitch3_angle',\n",
      "       'pitch2_moto_tmp', 'pitch3_moto_tmp', 'pitch1_ng5_tmp',\n",
      "       'pitch_angle_mean', 'pitch_angle_sd', 'pitch_moto_tmp_mean',\n",
      "       'pitch_moto_tmp_sd', 'r_wind_speed_to_power', 'r_square',\n",
      "       'wind_speed_square', 'wind_speed_cube'],\n",
      "      dtype='object')\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.218491Z",
     "start_time": "2024-12-23T03:16:16.583910Z"
    }
   },
   "cell_type": "code",
   "source": "rfe_features_21 = rfe_analyse(df_21)",
   "id": "76a8a8f446438585",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n",
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "rfe 选择的特征:\n",
      "Index(['generator_speed', 'pitch1_angle', 'pitch2_angle', 'pitch3_angle',\n",
      "       'pitch2_moto_tmp', 'pitch3_moto_tmp', 'environment_tmp',\n",
      "       'pitch2_ng5_tmp', 'pitch1_ng5_DC', 'pitch_angle_sd', 'tmp_diff', 'ct',\n",
      "       'r_wind_speed_to_power', 'r_wind_speed_to_generator_speed',\n",
      "       'wind_speed_square'],\n",
      "      dtype='object')\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\miniconda3\\envs\\pinn\\lib\\site-packages\\sklearn\\svm\\_base.py:297: ConvergenceWarning: Solver terminated early (max_iter=500).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.233687Z",
     "start_time": "2024-12-23T03:16:27.218993Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rfe_features = set(rfe_features_15).intersection(set(rfe_features_21))\n",
    "print('RFE特征交集')\n",
    "print(len(rfe_features)) \n",
    "print(rfe_features)"
   ],
   "id": "8442bc5d0610a0e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RFE特征交集\n",
      "7\n",
      "{'pitch3_angle', 'pitch3_moto_tmp', 'wind_speed_square', 'generator_speed', 'pitch_angle_sd', 'r_wind_speed_to_power', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总交集",
   "id": "1957f1b2ecca743c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.239384Z",
     "start_time": "2024-12-23T03:16:27.234690Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# corr_features = {'r_square', 'tmp_diff', 'pitch2_moto_tmp', 'environment_tmp', 'generator_speed', 'int_tmp', 'pitch3_angle', 'pitch_moto_tmp_mean', 'power', 'pitch3_moto_tmp', 'r_wind_speed_to_power', 'pitch1_moto_tmp', 'r_wind_speed_to_generator_speed'}\n",
    "# chi2_features = {'tmp_diff', 'r_square', 'pitch2_moto_tmp', 'pitch_speed_sd', 'environment_tmp', 'pitch2_angle', 'generator_speed', 'pitch_angle_mean', 'pitch1_angle', 'pitch3_angle', 'pitch_moto_tmp_mean', 'power', 'pitch3_moto_tmp', 'int_tmp', 'r_wind_speed_to_power', 'pitch1_moto_tmp', 'r_wind_speed_to_generator_speed'}\n",
    "# mi_features = {'tmp_diff', 'r_square', 'pitch2_moto_tmp', 'pitch_angle_sd', 'environment_tmp', 'pitch2_angle', 'pitch_angle_mean', 'pitch1_angle', 'pitch3_angle', 'pitch_moto_tmp_mean', 'int_tmp', 'pitch3_moto_tmp', 'pitch1_moto_tmp', 'r_wind_speed_to_power', 'yaw_position'}\n",
    "# lr_features = {'pitch2_moto_tmp', 'environment_tmp', 'pitch2_angle', 'wind_speed', 'generator_speed', 'pitch1_angle', 'pitch3_angle', 'pitch3_moto_tmp', 'r_wind_speed_to_power', 'pitch1_moto_tmp', 'r_wind_speed_to_generator_speed'}\n",
    "# random_forest_features = {'tmp_diff', 'r_square', 'pitch2_moto_tmp', 'pitch_angle_sd', 'environment_tmp', 'pitch1_angle', 'int_tmp', 'pitch_moto_tmp_mean', 'r_wind_speed_to_power', 'yaw_position'}\n",
    "# rfe_features = {'power', 'pitch_moto_tmp_mean', 'r_wind_speed_to_power', 'r_square', 'r_wind_speed_to_generator_speed', 'pitch3_angle', 'pitch2_angle', 'wind_speed', 'environment_tmp'}\n"
   ],
   "id": "9562c8513e3e2235",
   "outputs": [],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.248539Z",
     "start_time": "2024-12-23T03:16:27.240110Z"
    }
   },
   "cell_type": "code",
   "source": "all_features = corr_features.intersection(chi2_features).intersection(mi_features).intersection(lr_features).intersection(random_forest_features).intersection(rfe_features)",
   "id": "80397c9bd5dbc42e",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.258348Z",
     "start_time": "2024-12-23T03:16:27.249632Z"
    }
   },
   "cell_type": "code",
   "source": "all_features",
   "id": "87fad1d91b81030c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pitch2_moto_tmp', 'r_wind_speed_to_power'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.273713Z",
     "start_time": "2024-12-23T03:16:27.258995Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from collections import Counter\n",
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features = [\n",
    "    corr_features, chi2_features, mi_features, lr_features, random_forest_features, rfe_features\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter = Counter()\n",
    "for feature_set in all_features:\n",
    "    feature_counter.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features = sorted(feature_counter.items(), key=lambda x: x[1], reverse=True)\n",
    "\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features = [feature for feature, count in sorted_features if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(selected_features)"
   ],
   "id": "18b0d1e54d0005cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征出现频率排序\n",
      "[('r_wind_speed_to_power', 6), ('pitch2_moto_tmp', 6), ('pitch3_moto_tmp', 5), ('pitch3_angle', 5), ('power', 4), ('tmp_diff', 4), ('pitch1_moto_tmp', 4), ('int_tmp', 4), ('r_square', 4), ('wind_speed_square', 4), ('environment_tmp', 4), ('generator_speed', 4), ('pitch_moto_tmp_mean', 4), ('r_wind_speed_to_generator_speed', 3), ('pitch2_angle', 3), ('pitch_angle_mean', 3), ('pitch1_angle', 3), ('pitch_angle_sd', 3), ('yaw_position', 2), ('pitch_speed_sd', 1), ('wind_speed', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "['r_wind_speed_to_power', 'pitch2_moto_tmp', 'pitch3_moto_tmp', 'pitch3_angle', 'power', 'tmp_diff', 'pitch1_moto_tmp', 'int_tmp', 'r_square', 'wind_speed_square', 'environment_tmp', 'generator_speed', 'pitch_moto_tmp_mean', 'r_wind_speed_to_generator_speed', 'pitch2_angle', 'pitch_angle_mean', 'pitch1_angle', 'pitch_angle_sd']\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.278513Z",
     "start_time": "2024-12-23T03:16:27.274725Z"
    }
   },
   "cell_type": "code",
   "source": "print(len(selected_features))",
   "id": "3e4a630c61c4cac1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.288287Z",
     "start_time": "2024-12-23T03:16:27.279040Z"
    }
   },
   "cell_type": "code",
   "source": "print(selected_features.sort())",
   "id": "bd90a2a042b8592b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.298344Z",
     "start_time": "2024-12-23T03:16:27.289502Z"
    }
   },
   "cell_type": "code",
   "source": "print(selected_features)",
   "id": "ba60a103fa0695a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['environment_tmp', 'generator_speed', 'int_tmp', 'pitch1_angle', 'pitch1_moto_tmp', 'pitch2_angle', 'pitch2_moto_tmp', 'pitch3_angle', 'pitch3_moto_tmp', 'pitch_angle_mean', 'pitch_angle_sd', 'pitch_moto_tmp_mean', 'power', 'r_square', 'r_wind_speed_to_generator_speed', 'r_wind_speed_to_power', 'tmp_diff', 'wind_speed_square']\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.313688Z",
     "start_time": "2024-12-23T03:16:27.299004Z"
    }
   },
   "cell_type": "code",
   "source": "selected_features",
   "id": "5b4948c934a4b3f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_angle_sd',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'power',\n",
       " 'r_square',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'r_wind_speed_to_power',\n",
       " 'tmp_diff',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.329175Z",
     "start_time": "2024-12-23T03:16:27.314842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "mechanism_select_columns = ['environment_tmp', 'int_tmp', 'wind_speed', 'wind_speed_square', 'wind_speed_cube', 'yaw_speed', 'yaw_position', 'pitch_speed_mean', 'pitch_speed_sd', 'power', 'wind_direction', 'wind_direction_mean','tmp_diff']\n",
    "mechanism_select_columns.sort()\n",
    "print(mechanism_select_columns)\n",
    "mechanism_select_columns"
   ],
   "id": "b79e2b8cd17520a5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['environment_tmp', 'int_tmp', 'pitch_speed_mean', 'pitch_speed_sd', 'power', 'tmp_diff', 'wind_direction', 'wind_direction_mean', 'wind_speed', 'wind_speed_cube', 'wind_speed_square', 'yaw_position', 'yaw_speed']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'int_tmp',\n",
       " 'pitch_speed_mean',\n",
       " 'pitch_speed_sd',\n",
       " 'power',\n",
       " 'tmp_diff',\n",
       " 'wind_direction',\n",
       " 'wind_direction_mean',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square',\n",
       " 'yaw_position',\n",
       " 'yaw_speed']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.344606Z",
     "start_time": "2024-12-23T03:16:27.329992Z"
    }
   },
   "cell_type": "code",
   "source": "len(mechanism_select_columns)",
   "id": "4d63fbd8c35b79d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 取15号特征",
   "id": "1cd591b7298189d9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.358479Z",
     "start_time": "2024-12-23T03:16:27.345617Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features_15 = [\n",
    "    corr_features_15.index, chi2_features_15, mi_features_15, lr_features_15, rfe_features_15\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter_15 = Counter()\n",
    "for feature_set in all_features_15:\n",
    "    feature_counter_15.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features_15 = sorted(feature_counter_15.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features_15)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features_15 = [feature for feature, count in sorted_features_15 if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features_15)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features_15))\n",
    "print(selected_features_15)"
   ],
   "id": "82216fe3b7699cba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('r_wind_speed_to_power', 5), ('r_square', 5), ('pitch3_moto_tmp', 5), ('pitch2_moto_tmp', 5), ('pitch3_angle', 5), ('r_wind_speed_to_generator_speed', 4), ('environment_tmp', 4), ('generator_speed', 4), ('power', 4), ('int_tmp', 4), ('wind_speed_square', 4), ('pitch1_moto_tmp', 4), ('pitch_moto_tmp_mean', 4), ('tmp_diff', 3), ('pitch_moto_tmp_sd', 3), ('pitch1_angle', 3), ('pitch2_angle', 3), ('pitch_angle_mean', 3), ('yaw_position', 2), ('pitch_angle_sd', 2), ('pitch_speed_sd', 1), ('lambda', 1), ('wind_speed', 1), ('pitch1_ng5_tmp', 1), ('wind_speed_cube', 1)]\n",
      "特征出现频率排序\n",
      "[('r_wind_speed_to_power', 5), ('r_square', 5), ('pitch3_moto_tmp', 5), ('pitch2_moto_tmp', 5), ('pitch3_angle', 5), ('r_wind_speed_to_generator_speed', 4), ('environment_tmp', 4), ('generator_speed', 4), ('power', 4), ('int_tmp', 4), ('wind_speed_square', 4), ('pitch1_moto_tmp', 4), ('pitch_moto_tmp_mean', 4), ('tmp_diff', 3), ('pitch_moto_tmp_sd', 3), ('pitch1_angle', 3), ('pitch2_angle', 3), ('pitch_angle_mean', 3), ('yaw_position', 2), ('pitch_angle_sd', 2), ('pitch_speed_sd', 1), ('lambda', 1), ('wind_speed', 1), ('pitch1_ng5_tmp', 1), ('wind_speed_cube', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "18\n",
      "['r_wind_speed_to_power', 'r_square', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'pitch3_angle', 'r_wind_speed_to_generator_speed', 'environment_tmp', 'generator_speed', 'power', 'int_tmp', 'wind_speed_square', 'pitch1_moto_tmp', 'pitch_moto_tmp_mean', 'tmp_diff', 'pitch_moto_tmp_sd', 'pitch1_angle', 'pitch2_angle', 'pitch_angle_mean']\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.368255Z",
     "start_time": "2024-12-23T03:16:27.359724Z"
    }
   },
   "cell_type": "code",
   "source": "sorted(selected_features_15)",
   "id": "1a835177c3e29ce1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'power',\n",
       " 'r_square',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'r_wind_speed_to_power',\n",
       " 'tmp_diff',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 取21号特征",
   "id": "c9edca754305c55e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T03:16:27.383610Z",
     "start_time": "2024-12-23T03:16:27.368900Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features_21 = [\n",
    "    corr_features_21.index, chi2_features_21, mi_features_21, lr_features_21, rfe_features_21\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter_21 = Counter()\n",
    "for feature_set in all_features_21:\n",
    "    feature_counter_21.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features_21 = sorted(feature_counter_21.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features_21)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features_21 = [feature for feature, count in sorted_features_21 if count >= threshold]\n",
    "\n",
    "\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features_21)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features_21))\n",
    "print(selected_features_21)\n",
    "sorted(selected_features_21)"
   ],
   "id": "b47c8be99d294b7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('r_wind_speed_to_power', 5), ('tmp_diff', 5), ('pitch3_moto_tmp', 5), ('pitch2_moto_tmp', 5), ('pitch1_angle', 5), ('pitch2_angle', 5), ('pitch3_angle', 5), ('r_square', 4), ('environment_tmp', 4), ('r_wind_speed_to_generator_speed', 4), ('generator_speed', 4), ('pitch1_moto_tmp', 4), ('wind_speed_square', 4), ('power', 3), ('int_tmp', 3), ('pitch_moto_tmp_mean', 3), ('pitch_angle_mean', 3), ('yaw_position', 3), ('pitch_speed_sd', 2), ('wind_speed', 2), ('wind_speed_cube', 2), ('pitch_angle_sd', 2), ('pitch_moto_tmp_sd', 1), ('pitch2_ng5_tmp', 1), ('pitch1_ng5_DC', 1), ('ct', 1)]\n",
      "特征出现频率排序\n",
      "[('r_wind_speed_to_power', 5), ('tmp_diff', 5), ('pitch3_moto_tmp', 5), ('pitch2_moto_tmp', 5), ('pitch1_angle', 5), ('pitch2_angle', 5), ('pitch3_angle', 5), ('r_square', 4), ('environment_tmp', 4), ('r_wind_speed_to_generator_speed', 4), ('generator_speed', 4), ('pitch1_moto_tmp', 4), ('wind_speed_square', 4), ('power', 3), ('int_tmp', 3), ('pitch_moto_tmp_mean', 3), ('pitch_angle_mean', 3), ('yaw_position', 3), ('pitch_speed_sd', 2), ('wind_speed', 2), ('wind_speed_cube', 2), ('pitch_angle_sd', 2), ('pitch_moto_tmp_sd', 1), ('pitch2_ng5_tmp', 1), ('pitch1_ng5_DC', 1), ('ct', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "18\n",
      "['r_wind_speed_to_power', 'tmp_diff', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'pitch1_angle', 'pitch2_angle', 'pitch3_angle', 'r_square', 'environment_tmp', 'r_wind_speed_to_generator_speed', 'generator_speed', 'pitch1_moto_tmp', 'wind_speed_square', 'power', 'int_tmp', 'pitch_moto_tmp_mean', 'pitch_angle_mean', 'yaw_position']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'power',\n",
       " 'r_square',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'r_wind_speed_to_power',\n",
       " 'tmp_diff',\n",
       " 'wind_speed_square',\n",
       " 'yaw_position']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总交集",
   "id": "9d005bbb0f08873c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T06:00:27.513604Z",
     "start_time": "2024-12-23T06:00:27.500299Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_features = all_features_15 + all_features_21\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter = Counter()\n",
    "for feature_set in all_features:\n",
    "    feature_counter.update(feature_set)\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features = sorted(feature_counter.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 5\n",
    "selected_features = [feature for feature, count in sorted_features if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features))\n",
    "print(selected_features)"
   ],
   "id": "25897dae426fc2be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('r_wind_speed_to_power', 10), ('pitch3_moto_tmp', 10), ('pitch2_moto_tmp', 10), ('pitch3_angle', 10), ('r_square', 9), ('r_wind_speed_to_generator_speed', 8), ('environment_tmp', 8), ('generator_speed', 8), ('wind_speed_square', 8), ('tmp_diff', 8), ('pitch1_moto_tmp', 8), ('pitch1_angle', 8), ('pitch2_angle', 8), ('power', 7), ('int_tmp', 7), ('pitch_moto_tmp_mean', 7), ('pitch_angle_mean', 6), ('yaw_position', 5), ('pitch_moto_tmp_sd', 4), ('pitch_angle_sd', 4), ('pitch_speed_sd', 3), ('wind_speed', 3), ('wind_speed_cube', 3), ('lambda', 1), ('pitch1_ng5_tmp', 1), ('pitch2_ng5_tmp', 1), ('pitch1_ng5_DC', 1), ('ct', 1)]\n",
      "特征出现频率排序\n",
      "[('r_wind_speed_to_power', 10), ('pitch3_moto_tmp', 10), ('pitch2_moto_tmp', 10), ('pitch3_angle', 10), ('r_square', 9), ('r_wind_speed_to_generator_speed', 8), ('environment_tmp', 8), ('generator_speed', 8), ('wind_speed_square', 8), ('tmp_diff', 8), ('pitch1_moto_tmp', 8), ('pitch1_angle', 8), ('pitch2_angle', 8), ('power', 7), ('int_tmp', 7), ('pitch_moto_tmp_mean', 7), ('pitch_angle_mean', 6), ('yaw_position', 5), ('pitch_moto_tmp_sd', 4), ('pitch_angle_sd', 4), ('pitch_speed_sd', 3), ('wind_speed', 3), ('wind_speed_cube', 3), ('lambda', 1), ('pitch1_ng5_tmp', 1), ('pitch2_ng5_tmp', 1), ('pitch1_ng5_DC', 1), ('ct', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "18\n",
      "['r_wind_speed_to_power', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'pitch3_angle', 'r_square', 'r_wind_speed_to_generator_speed', 'environment_tmp', 'generator_speed', 'wind_speed_square', 'tmp_diff', 'pitch1_moto_tmp', 'pitch1_angle', 'pitch2_angle', 'power', 'int_tmp', 'pitch_moto_tmp_mean', 'pitch_angle_mean', 'yaw_position']\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-23T06:00:28.699252Z",
     "start_time": "2024-12-23T06:00:28.683100Z"
    }
   },
   "cell_type": "code",
   "source": "sorted(selected_features)",
   "id": "83ecc667d251464d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'power',\n",
       " 'r_square',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'r_wind_speed_to_power',\n",
       " 'tmp_diff',\n",
       " 'wind_speed_square',\n",
       " 'yaw_position']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "8a73e7e4027bfc7c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
